// education-form.js
Page({
    data: {
        options: {},
        formData: {
            startDate: '',
            endDate: '',
            degreeIndex: 0,
            degree: '',
            schoolName: '',
            major: ''
        },
        degreeOptions: ['其他', '高中', '中专', '大专', '本科', '硕士', '博士']
    },
    onLoad: function (options) {
        // console.log(options);
        this.setData({
            options: options // 将 options 存储在 data 中
        });
        if (options && options.index) {
            // const education = JSON.parse(options.education);
            // 从本地存储中获取表单数据
            wx.getStorage({
                key: 'educationList',
                success: (res) => {
                    const {data} = res
                    const education = data[options.index]
                    console.log(education)
                    this.setData({
                        formData: {
                            startDate: education.startDate,
                            endDate: education.endDate,
                            degreeIndex: education.degreeIndex,
                            degree: education.degree || education.degreeIndex[education.degreeIndex],
                            schoolName: education.schoolName,
                            major: education.major
                        }
                    });
                },
                fail: (err) => {
                    console.log('获取个人表单数据失败', err);
                }
            });

        }
    },
    startChange: function (e) {
        console.log(e)
        this.setData({
            'formData.startDate': e.detail.value.slice(0, -3)
        });
    },
    endChange: function (e) {
        this.setData({
            'formData.endDate': e.detail.value.slice(0, -3)
        });
    },
    schoolNameChange: function (e) {
        this.setData({
            'formData.schoolName': e.detail.value
        });
    },
    majorChange: function (e) {
        this.setData({
            'formData.major': e.detail.value
        });
    },
    degreeChange: function (e) {
        console.log(e.detail.value)
        this.setData({
            'formData.degreeIndex': e.detail.value,
            'formData.degree': this.data.degreeOptions[e.detail.value]
        });
    },
    submitForm: function (e) {
        console.log(this.data.options)
        const educationList = wx.getStorageSync('educationList') || [];
        if (this.data.options && this.data.options.index) {
            educationList[this.data.options.index] = this.data.formData;
        } else {
            educationList.push(this.data.formData);
        }
        wx.setStorageSync('educationList', educationList);
        wx.navigateBack();
    }
});
